
// pages/classroom/classroom_details/classroom_details.js
import request from "../../../utils/request";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dateList:[],
    navList:[
      {'nav':'借用信息'},
      {'nav':'设备信息'}
    ],
    classroomInfo:'',
    clickId:0,
    navTo:1,
    navId:0,
    click_date:'',
    details:'',
    equList:'',
    classroom_id:'',
    click_week:''
  },
  /**
   * 改变背景颜色
   */
  tabClick(e){
    var nowTime= this.getCurrentMonthFirst()
    this.setData({
      clickId: e.currentTarget.id,
      click_date: nowTime.substring(0,5)+e.currentTarget.dataset.date,
      click_week: e.currentTarget.dataset.week,
    })
    console.log(this.data.click_date)
    this.getReservationInfo(this.data.classroom_id,this.data.click_date)
  },
  navClick(e){
    this.setData({
      navId: e.currentTarget.id
    })
    if(e.currentTarget.id==1){
      this.setData({
        navTo:0,
      })
    }else{
      this.setData({
        navTo:1,
      })
    }
  },
  getEqupmentInfor(e){
    this.getClassroomEqu(this.data.classroom_id);
  },
  getReverstionInfor(e){
    this.getReservationInfo(this.data.classroom_id,this.data.click_date);
  },

  jumpDetails:function(e){
    var peroid = e.currentTarget.dataset.peroid;
    if(wx.getStorageSync('userInfo')==''){
      wx.reLaunch({
        url: '../../login/login',
      })
      return;
   }
    wx.navigateTo({
      url: '../reservation_details/reservation_details?peroid='+peroid+'&classroom_id='+this.data.classroom_id+'&date='+this.data.click_date+'&week='+this.data.click_week,
    })
    },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      classroom_id:options.classroomId
    })
    this.getClassroom(this.data.classroom_id);
    this.setData({
      dateList:JSON.parse(JSON.stringify(this.getDates(5,this.getCurrentMonthFirst())))
    })
    if(this.data.click_date=="" && this.data.click_week==""){
      this.setData({
        click_date:this.getCurrentMonthFirst(),
        click_week:this.getDates(1,this.getCurrentMonthFirst())[0].week
      })
    }
     this.getReservationInfo(this.data.classroom_id,this.data.click_date);
  },
  /**
   * 
   * @param {获取教室信息}} key 
   */
  async  getClassroom(key){
    let result = await request('classroom_ms1.0/classroom/search',{key},'GET');
    console.log(result);
    var data = result.data;
    this.setData({
        classroomInfo:data[0]
    })
  },
  /**
   * 
   * @param {获取教室设备信息} key 
   */
  async  getClassroomEqu(classroom_id){
    let result = await request('classroom_ms1.0/equipment/getByClassroom',{classroom_id},'GET');
    console.log(result);
    var data = result.data;
    this.setData({
        equList:data
    })
  },

  /**
   * 
   * @param {获取教室借用信息} key 
   */
  async  getReservationInfo(classroom_id,date){
    let result = await request('classroom_ms1.0/reservation/getReserinfo',{classroom_id,date},'GET');
    console.log(result);
    var data = result.data;
    this.setData({
        details:data
    })
    this.onLoad()
  },
  /**
   * 改变颜色
   */

  /**
   * 日期数据处理
   */
 
  //获取当前时间多少天后的日期和对应星期
  getDates(days,todate) {//todate默认参数是当前日期，可以传入对应时间
    var dateArry = [];
    for (var i = 0; i < days; i++) {
      var dateObj = this.dateLater(todate, i);
      dateArry.push(dateObj)
    }
    return dateArry;
  },
  /**
     * 传入时间后几天
     * param：传入时间：dates:"2018-04-02",later:往后多少天
     */
  dateLater(dates, later) {
    let dateObj = {};
    let show_day = new Array('星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六');
    let date = new Date(dates);
    date.setDate(date.getDate() + later);
    let day = date.getDay();
    dateObj.date =((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1)+"-"+(date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    dateObj.week = show_day[day];
    return dateObj;
  },
  //获取当前时间
  getCurrentMonthFirst() {
    var date = new Date();
    var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
    return todate;
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
      
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})